package com.denso.workflow.admin.image;

import com.denso.workflow.common.model.Image;
import com.denso.workflow.interceptor.AdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.upload.UploadFile;
import com.xiaoleilu.hutool.date.DateUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by lijian on 2018/9/22.
 */
@Before(AdminInterceptor.class)
public class ImageController extends Controller {
    static final ImageService service = new ImageService();

    public void index() {
        List<Image> imgList = service.findAll();
        setAttr("imgList", imgList);
        render("index.html");
    }

    public void add() {
        render("add.html");
    }

    public void save() {
        UploadFile upFile = getFile();

        String inputCode = getPara("code");
        int codeBegin = PropKit.getInt("codeBegin");
        int codeEnd = PropKit.getInt("codeEnd");
        String code = inputCode.substring(codeBegin, codeEnd);

        // 品番要做截取处理

        // 检查code重复
        List<Image> result = service.findByCode(code);
        if(result != null && result.size() > 0) {
            setAttr("notice", "此品番零件在系统内已登记,请检查后重新提交");
            render("add.html");
            return;
        } else {
            // 匹配供应商品番匹配规则
            String pcode = getPara("pcode");
            int beginIndex = 0;
            int endIndex = 0;
            if(!pcode.trim().equals("")) {
                beginIndex = pcode.indexOf(code);
                if (beginIndex == -1) {
                    // 供应商号码中未能匹配到品番
                    setAttr("notice", "供应商品番中不包含TFDACC品番号,请检查后重新提交");
                    render("add.html");
                    return;
                }
                endIndex = beginIndex + code.length();
            }

            String upFileName = upFile.getFileName();
            String endFix = upFileName.substring(upFileName.lastIndexOf('.'));
            try {
                FileInputStream fin = new FileInputStream(upFile.getFile());
                byte[] data = new byte[fin.available()];
                fin.read(data);
                fin.close();

                String newFileName = code + "_" + DateUtil.format(new Date(), "MMddHHmm") + endFix;
                FileOutputStream fous = new FileOutputStream(PathKit.getWebRootPath() + "/image/" + newFileName);
                fous.write(data);
                fous.flush();
                fous.close();

                try {
                    upFile.getFile().delete();
                } catch (Exception ex) {
                    // 删除上传文件失败, do nothing
                }

                Image image = new Image();
                image.setCode(code);
                image.setImg(newFileName);
                image.setPcodeBegin(beginIndex);
                image.setPcodeEnd(endIndex);

                service.save(image);
                setAttr("notice", "指示图添加成功");
            } catch (Exception ex) {
                setAttr("notice", "保存图片失败:" + ex.toString());
            }
            index();
        }

    }


    public void updatePcode() {
        int id = getParaToInt("id");

        Image image = service.findById(id);
        setAttr("image", image);

        render("updatepcode.html");
    }

    public void savePcode() {
        int id = getParaToInt("id");
        String code = getPara("code");

        // 匹配供应商品番匹配规则
        String pcode = getPara("pcode");
        int beginIndex = 0;
        int endIndex = 0;

        beginIndex = pcode.indexOf(code);
        if (beginIndex == -1) {
            // 供应商号码中未能匹配到品番
            setAttr("notice", "供应商品番中不包含TFDACC品番号,请检查后重新提交");
            Image image = service.findById(id);
            setAttr("image", image);

            render("updatepcode.html");
        } else {
            endIndex = beginIndex + code.length();

            boolean res = service.savePcode(id, beginIndex, endIndex);
            String notice;
            if (res) {
                notice = "供应商品番更新成功";
            } else {
                notice = "供应商品番更新失败";
            }
            setAttr("notice", notice);
            index();
        }
    }

    public void delete() {
        int id = getParaToInt("id");

        service.deleteById(id);
        try {
            Image image = service.findById(id);
            File imageFile = new File(PathKit.getWebRootPath() + "/image/" + image.getImg());
            imageFile.delete();
        } catch (Exception ex) {
            // 删除图片失败 do nothing
        }

        setAttr("notice", "指示图删除成功");
        index();
    }
}
